package cn.tqfeiyang.smartadmin.business.organization.controller;

import cn.tqfeiyang.smartadmin.business.organization.constant.EmployeeConstants;
import cn.tqfeiyang.smartadmin.business.organization.service.EmployeeAssignService;
import cn.tqfeiyang.smartadmin.system.response.RoleQueryVO;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.List;

/**
 * 员工分配控制器
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@RestController
@Validated
@RequestMapping(EmployeeConstants.REQUEST_URL)
@Tag(name = EmployeeConstants.API_TAG)
public class EmployeeAssignController {

    /**
     * 员工分配服务
     */
    @Resource
    private EmployeeAssignService employeeAssignService;

    /**
     * 查询角色列表
     */
    @OperationLog
    @GetMapping("/queryRolesByEmployeeId/{employeeId}")
    @Operation(summary = "员工 - 查询角色列表")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "queryRolesByEmployeeId")
    public ApiResult<List<RoleQueryVO>> queryRolesByEmployeeId(@PathVariable @EntityId Long employeeId) {
        List<RoleQueryVO> list = employeeAssignService.queryRolesByEmployeeId(employeeId);
        return ApiResult.success(list);
    }

    /**
     * 查询角色id列表
     */
    @OperationLog
    @GetMapping("/getRoleIdsByEmployeeId/{employeeId}")
    @Operation(summary = "员工 - 查询角色id列表")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "getRoleIdsByEmployeeId")
    public ApiResult<List<Long>> getRoleIdsByEmployeeId(@PathVariable @EntityId Long employeeId) {
        List<Long> list = employeeAssignService.getRoleIdsByEmployeeId(employeeId);
        return ApiResult.success(list);
    }

    /**
     * 分配角色id列表
     */
    @OperationLog
    @PostMapping("/assignRoleIdsByEmployeeId/{employeeId}")
    @Operation(summary = "员工 - 分配角色id列表")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "assignRoleIdsByEmployeeId")
    public ApiResult assignRoleIdsByEmployeeId(@PathVariable @EntityId Long employeeId, @RequestBody @Validated List<Long> roleIds) {
        employeeAssignService.assignRoleIdsByEmployeeId(employeeId, roleIds);
        return ApiResult.success();
    }

    /**
     * 删除角色id列表
     */
    @OperationLog
    @PostMapping("/deleteRoleIdsByEmployeeId/{employeeId}")
    @Operation(summary = "员工 - 删除角色id列表")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "deleteRoleIdsByEmployeeId")
    public ApiResult deleteRoleIdsByEmployeeId(@PathVariable @EntityId Long employeeId, @RequestBody @Validated List<Long> roleIds) {
        employeeAssignService.deleteRoleIdsByEmployeeId(employeeId, roleIds);
        return ApiResult.success();
    }

    /**
     * 清空所有分配的角色id
     */
    @OperationLog
    @PostMapping("/deleteAllRoleIdsByEmployeeId/{employeeId}")
    @Operation(summary = "员工 - 清空所有分配的角色id")
    @SaCheckPermission(EmployeeConstants.PERMISSION + "deleteAllRoleIdsByEmployeeId")
    public ApiResult deleteAllRoleIdsByEmployeeId(@PathVariable @EntityId Long employeeId) {
        employeeAssignService.deleteAllRoleIdsByEmployeeId(employeeId);
        return ApiResult.success();
    }
}
